﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class SelectRole : MonoBehaviour
{
    private GameObject _roleListContent;
    private ToggleGroup _roleListToggleGroup;
    private Button _btnEnter;

    private GameObject _modelStudio;//模型摄影棚
    private GameObject _modelPlace;//模型放置节点

    private TouchRotate _modelTouchRotate;

    private int _selectRoleIndex=-1;
    private int _lastRoleIndex = 1;//假设上一次服务器记录选中的是1
    // Start is called before the first frame update
    void Awake()
    {

        _roleListContent = transform.Find
            ("RoleList/Viewport/Content").gameObject;
        _roleListToggleGroup= _roleListContent.GetComponent<ToggleGroup>();

        _btnEnter = transform.Find
            ("BtnEnter").GetComponent<Button>();
        //注册按键事件
        _btnEnter.onClick.AddListener(onBtnEnterClick);


        //处理模型摄影棚部分
        _modelStudio = GameObject.Instantiate
            (Resources.Load<GameObject>("UI/SelectRole/ModelStudio"));
        _modelPlace = _modelStudio.Find<Transform>("ModelPlace").gameObject;
        _modelTouchRotate = gameObject.Find<TouchRotate>("TouchRotate");
        _modelTouchRotate.DragCallBack = onTouchRotate;
        print(_modelPlace.name);


        //初始化角色列表
        int i = 0;
        //foreach (var roleInfo in UserData.instance.AllRole)
        foreach (var roleInfo in RoleTable.instance.GetAll())
        {
            var roleItem =GameObject.Instantiate
                ( Resources.Load<GameObject>("UI/SelectRole/RoleItem"));
            roleItem.transform.SetParent(_roleListContent.transform,false);
            var textName = roleItem.transform.Find("Label").GetComponent<Text>();
            var toggle = roleItem.GetComponent<Toggle>();

            textName.text = roleInfo.Value.Name;

            toggle.group = _roleListToggleGroup;
            //用闭包实现角色索引和toggle绑定
            var index = i;
            i++;
            toggle.onValueChanged.AddListener((isOn)=> { onToggleValueChange(index, isOn); });
            //默认选中上一次服务器记录的角色
            if( index == _lastRoleIndex)
            {
                print(index);
                toggle.Select();
                toggle.isOn = true;
            }
                
        }

    }

    private void onTouchRotate(PointerEventData eventData)
    {
       _modelPlace.transform.Rotate(new Vector3(0,-eventData.delta.x, 0));
    }

    private void onToggleValueChange(int roleIndex, bool isOn)
    {
        //Debug.Log(string.Format("{0},{1}", roleIndex, isOn));
        if (isOn)
        {
            //先清除之前的模型(清空ModelPlace下面的所有物体)
            _modelPlace.DestroyAllChildren();//这个方法是在UnityExtern类扩展的
            //
         

            //2

            //记录选择的索引

            _selectRoleIndex = roleIndex;
            var curRoleInfo = UserData.instance.AllRole[roleIndex];
            var model = GameObject.Instantiate(Resources.Load<GameObject>(curRoleInfo.ModelResPath));
            model.transform.SetParent(_modelPlace.transform, false);
        }
    }

    private void onBtnEnterClick()
    {
        throw new NotImplementedException();
    }
}
